package org.leetcodeplus.leetcodeplus.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.leetcodeplus.leetcodeplus.api.entity.LcTest;
import org.leetcodeplus.leetcodeplus.api.entity.LcTestVo;
import org.leetcodeplus.leetcodeplus.api.mapper.LcTestMapper;
import org.leetcodeplus.leetcodeplus.api.service.ILcTestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-31
 */
@Service
public class LcTestServiceImpl extends ServiceImpl<LcTestMapper, LcTest> implements ILcTestService {

    @Autowired
    private LcTestMapper lcTestMapper;


    @Override
    public List<LcTest> findExistingTests(Collection<LcTest> lcTests) {
        List<String> testCodes = lcTests.stream().map(LcTest::getTestCode).collect(Collectors.toList());
        return lcTestMapper.findByTestCodesIn(testCodes);
    }

    @Override
    public List<LcTest> getTests(String parentId, String status) {
        QueryWrapper<LcTest> queryWrapper = new QueryWrapper<>();
        if (parentId != null) {
            queryWrapper.eq("parent_id", parentId);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByAsc("id");
        return lcTestMapper.selectList(queryWrapper);
    }

    @Override
    public LcTestVo getLcByCode(String code) {

        if(code!=null){
            LcTestVo lcTestVo = new LcTestVo();

            LcTest lcByCode = lcTestMapper.getLcByCode(code);

            //查出对应的题目中文
            String testCode= lcByCode.getParentId();
            String test = lcTestMapper.getTestCode(testCode);
            lcTestVo.setTestName(test);

            //查出对应的状态中文
            String testStatus= lcByCode.getStatus();
            String status = lcTestMapper.getTestCode(testStatus);
            lcTestVo.setStatusName(status);

            return lcTestVo;
        }

        return null;


    }
}
